package com.lyl.syt.api;

import com.lyl.syt.common.result.Result;
import com.lyl.syt.common.utils.AuthContextHolder;
import com.lyl.syt.model.user.Patient;
import com.lyl.syt.service.PatientService;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/api/user/patient")
@RequiredArgsConstructor
public class PatientApiController {
    private final PatientService patientService;

    //根据就诊人id获取就诊人信息
    @ApiOperation("根据就诊人id获取就诊人信息")
    @GetMapping("/inner/get/{id}")
    public Patient getPatientOrder(@PathVariable Long id){
        return patientService.getPatientById(id);
    }

    //获取就诊人列表
    @ApiOperation("获取就诊人列表")
    @GetMapping("/auth/findAll")
    public Result<List<Patient>> findAll(HttpServletRequest request){
        //获取当前登录用户id
        Long userId = AuthContextHolder.getUserId(request);
        List<Patient> list = patientService.findAllByUserId(userId);
        return Result.ok(list);
    }

    //添加就诊人
    @ApiOperation("添加就诊人")
    @PostMapping("/auth/save")
    public Result<Boolean> savePatient(@RequestBody Patient patient,HttpServletRequest request){
        //获取当前登录用户id
        Long userId = AuthContextHolder.getUserId(request);
        patient.setUserId(userId);
        patientService.save(patient);
        return Result.ok();
    }

    //根据id获取就诊人信息
    @ApiOperation("根据id获取就诊人信息")
    @GetMapping("/auth/get/{id}")
    public Result<Patient> getPatientById(@PathVariable Long id){
        return Result.ok(patientService.getPatientById(id));
    }

    //修改就诊人信息
    @ApiOperation("修改就诊人信息")
    @PutMapping("/auth/update")
    public Result<Boolean> updatePatient(@RequestBody Patient patient){
        patientService.updateById(patient);
        return Result.ok();
    }

    //删除就诊人
    @ApiOperation("删除就诊人")
    @DeleteMapping("/auth/remove/{id}")
    public Result<Boolean> deletePatient(@PathVariable Long id){
        patientService.removeById(id);
        return Result.ok();
    }




}
